package validates

import (
	"myapp/dal/model"
	"myapp/dal/query"
	"myapp/pkg"
	"strings"
	"time"
)

type UserProfile struct {
	UserID      int32    `json:"user_id"`
	Nickname    string   `json:"nickname" binding:"required" label:"网名"` // 必填字段
	Gender      string   `json:"gender" binding:"required" label:"性别"`
	BirthYear   int32    `json:"birth_year" binding:"required" label:"出生年份"`
	Income      string   `json:"income" binding:"required" label:"收入"`
	Avatar      string   `json:"avatar" label:"头像"`
	Province    string   `json:"province" binding:"required" label:"省份"`
	City        string   `json:"city" binding:"required" label:"城市"`
	District    string   `json:"district" binding:"required" label:"地区"`
	DatingType  string   `json:"dating_type" binding:"required" label:"交友类型"`
	Interests   []string `json:"interests" binding:"required" label:"兴趣方向"`
	Description string   `json:"description" binding:"required" label:"交友说明"`
	Photos      []string `json:"photos" binding:"required" label:"照片"`
	Age         int      `json:"age"`
	Time        string   `json:"time"`
	IsCalled    bool     `json:"is_called"`
}

// 处理函数：将 `Need` 转换为 `CustomNeed`，并添加额外字段
func ProcessUserProfile(nd *model.UserProfile, user_id int) UserProfile {
	images := strings.Split(nd.Photos, ",")

	age := time.Now().Year() - int(nd.Birthday)
	// 返回自定义结构体
	time, _ := pkg.FormatTime(nd.CreatedAt.Format("2006-01-02 15:04:05"), false)

	call, _ := query.Call.Where(query.Call.ObjUserID.Eq(nd.UserID), query.Call.UserID.Eq(int32(user_id))).Take()
	is_called := false
	if call != nil {
		is_called = true
	}

	return UserProfile{
		UserID:      nd.UserID,
		Description: nd.Description,
		Time:        time,
		Photos:      images,
		Nickname:    nd.Nickname,
		Age:         age,
		City:        nd.City,
		Avatar:      nd.Avatar,
		DatingType:  nd.DatingType,
		Income:      nd.Income,
		IsCalled:    is_called,
	}
}
